﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using EncryptHelper;
using System.IO;
using System.Reflection;

namespace CustomersSuppliersWebApplication
{
    public enum NotificationIconType
    {
        Ok,
        Error,
        Warning
    }
    public class WebFormBase : Page
    {
        public WebFormBase():base()
        {
            WebHelper.Modules.SecurityModule.DecryptNeeded("");
            this.Load += WebFormBase_Load;
        }

        void WebFormBase_Load(object sender, EventArgs e)
        {
            string LicencePath = Server.MapPath("~/Licence.lic");
            if (!File.Exists(LicencePath))
            {
                if (!this.Request.Url.ToString().ToLower().Contains("WebFormHome.aspx".ToLower()))
                {
                    this.Response.Redirect("~/WebFormHome.aspx");
                }
            }
        }
        

        protected string PageTitle
        {
            get
            {
                string s="";
                if (this.Master != null)
                {
                    RadButton button = (RadButton)this.Master.FindControl("RadButtonTitle");
                    if (button != null)
                    {
                        s = button.Text;
                    }
                }
                return s;
            }
            set
            {
                if (this.Master != null)
                {
                    RadButton button = (RadButton)this.Master.FindControl("RadButtonTitle");
                    if (button != null)
                    {
                        button.Text=value;
                    }
                }
            }
        }

        protected void InitializeTelerikRequest(WebControl control, string attribute)
        {
            string script = "function DoTelerikRequest(ElementUniqueID)"
                            + "{"
                            + "$find('" + RadAjaxManager.GetCurrent(this.Page).ClientID + "').ajaxRequestWithTarget(ElementUniqueID, '');"
                            + "}";
            RadScriptManager.RegisterStartupScript(this, this.GetType(), "DoTelerikRequest", script, true);
            control.Attributes[attribute] = "DoTelerikRequest('" + control.UniqueID + "');";
        }
        protected void ShowNotification(string text, NotificationIconType notificationIconType)
        {
            if (this.Master != null)
            {
                RadNotification defaultNotification = null;
                if (notificationIconType == NotificationIconType.Error)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationErrorDefault");
                }
                else if (notificationIconType == NotificationIconType.Ok)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationOkDefault");
                }
                else if (notificationIconType == NotificationIconType.Warning)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationWarningDefault");
                }
                if (defaultNotification != null)
                {
                    defaultNotification.Show(text);
                }
            }
        }
        protected void ShowWindow(string url)
        {
            string script = string.Format("window.radopen('{0}', null);",url);
            RadScriptManager.RegisterStartupScript(this,this.GetType(),"OpenWindow",script,true);
        }
        protected void DoPostPackInsteadAjax(WebControl control)
        {
            RadScriptManager.GetCurrent(this.Page).RegisterPostBackControl(control);
        }
    }
}